﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace WooCoo.ObjectModel.Validation
{
    [Serializable]
    public class BrokenRulesCollection:Collection<BrokenRule>
    {

        /// <summary>
        /// 清除指定属性的错误规则
        /// </summary>
        /// <param name="propertyName"></param>
        public void ClearBrokenRules(string propertyName)
        {
            var br = this.Where(c => c.PropertyName == propertyName).ToList();
            foreach (var item in br)
            {
                Remove(item);
            }
        }

        #region 显示

        /// <summary>
        /// Returns the text of all broken rule descriptions, each
        /// separated by a <see cref="Environment.NewLine" />.
        /// </summary>
        /// <returns>The text of all broken rule descriptions.</returns>
        public override string ToString()
        {
            return this.ToString(Environment.NewLine);
        }
       
        /// <summary>
        /// Returns the text of all broken rule descriptions.
        /// </summary>
        /// <param name="separator">
        /// String to place between each broken rule description.
        /// </param>
        /// <returns>The text of all broken rule descriptions.</returns>
        public string ToString(string separator)
        {
            return string.Join(separator, this.Select(r => r.ErrorMessage));
        }       

        #endregion
    }
}
